package com.seek.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.ResponseResult;
import com.seek.eduservice.entity.EduTeacher;
import com.seek.eduservice.entity.vo.TeacherQuery;
import com.seek.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-01-09
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    // 1. 查询讲师表所有数据
    @ApiOperation("所有讲师列表")
    @GetMapping
    public ResponseResult findAllTeacher() {
        // 调用 service 的方法实现查询所有的操作
        // 测试自定义异常
//        try {
//            int a = 10 / 0;
//        } catch (Exception e) {
//            throw new SeekException(20001, "执行了 SeekException 自定义异常");
//        }
        List<EduTeacher> eduTeacherList = eduTeacherService.list();
        return ResponseResult.ok().data("items", eduTeacherList);
    }

    // 2. 逻辑删除讲师的方法
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("{id}")
    public ResponseResult removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean result = eduTeacherService.removeById(id);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

    // 3. 分页查询讲师方法
    @ApiOperation("分页查询讲师")
    @GetMapping("page/{current}/{size}")
    public ResponseResult pageTeacher(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "size", value = "当前页显示行数", required = true) @PathVariable long size) {
        Page<EduTeacher> page = new Page<>(current, size);
        eduTeacherService.page(page);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        // 通过 page 对象获取分页数据
        System.out.println(page.getCurrent()); // 当前页
        System.out.println(page.getRecords()); // 每页数据 list 集合
        System.out.println(page.getSize()); // 每页显示记录数
        System.out.println(page.getTotal()); // 总记录数
        System.out.println(page.getPages()); // 总页数
        System.out.println(page.hasNext()); // 是否有下一页
        System.out.println(page.hasPrevious()); // 是否有上一页

//        Map<String, Object> map = new HashMap<>();
//        map.put("total", total);
//        map.put("records", records);
//        return ResponseResult.ok().data(map);
        System.out.println("======" + total);
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 4. 条件查询带分页的方法
    @ApiOperation("条件查询带分页")
    @GetMapping("pageCondition/{current}/{size}")
    public ResponseResult pageCondition(@PathVariable int current, @PathVariable int size, TeacherQuery teacherQuery) {
        Page<EduTeacher> page = new Page<>(current, size);
        // 构建条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        // 判断条件值是否为空，如果不为空拼接条件
        if (!(name == null || name.equals(""))) {
            queryWrapper.like("name", name);
        }
        if (level != null) {
            queryWrapper.eq("level", level);
        }
        if (!(begin == null || begin.equals(""))) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!(end == null || end.equals(""))) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page, queryWrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 5. 添加讲师接口的方法
    @ApiOperation("添加讲师")
    @PostMapping("add")
    public ResponseResult add(@RequestBody EduTeacher eduTeacher) {
        boolean result = eduTeacherService.save(eduTeacher);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

    // 6. 根据讲师 id 进行查询
    @ApiOperation("根据讲师 id 进行查询")
    @GetMapping("get/{id}")
    public ResponseResult getById(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return ResponseResult.ok().data("eduTeacher", eduTeacher);
    }

    // 7. 讲师修改功能
    @ApiOperation("根据讲师 id 进行修改")
    @PutMapping("update")
    public ResponseResult updateById(EduTeacher eduTeacher) {
        boolean result = eduTeacherService.updateById(eduTeacher);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

}

